
$(document).ready(function(){
	var Main = {
			created(){
				this.getSignupInfo();
				this.getActivityXiangmus();
			},
			filters:{
				formateDate(time){
					return new Date(time).Format("yyyy-MM-dd");
				}
			},
		    data () {
				return {
					nowDate:new Date().getTime(),
					ctx:ctx,
					bmId:bmId,
					bmzzList:[],
					activityId:activityId,
					activity:"",
					xuesheng:"",
					daimas:[],
					activityXiangmu:null,
					studentName:"",
					xueshengs:[],
					activityXiangmus:[],
					xueXiaos:[],
					keyword:"",
					saveData:{
						studentId:"",
						xuexiaoId:"",
						sbr:"",
						dmid:"",
						activityXiangmuId:"",
						ssxx:"",
						bmzz:"",
						zdls:"",
						id:"",
						activityId:activityId,
						c1:"",
						c2:"",
						c3:"",
						c4:"",
						c5:"",
						c6:""
					}
				}
			},
			methods:{
				//添加参赛人
				addBmzz(){
					let that =this;
					let arr = $(".ck");
					$(".ck").each(function(i,o){
						if($(o).is(':checked')){
							let name = $(o).attr("data-stuName");
							if(that.bmzzList.indexOf(name) == -1){
								that.bmzzList.push(name);
							}
						}
					});
				},
				removeBmzz(name){
					this.bmzzList.remove(name);
				},
				getSignupInfo(){
					let that = this;
					$.ajax({
						type: "POST",
						url: ctx + "/getSignupInfo.do",
						data: {activityId:that.activityId},
						success: function(data) {
							if(data.state){
								that.activity = data.activity;
								that.xuesheng = data.xuesheng;
								that.saveData.studentId = data.xuesheng.id;
								that.saveData.sbr = data.xuesheng.xm;
								that.saveData.bmzz = data.xuesheng.xm;
								that.saveData.ssxx = data.xuesheng.xueXiao.xxmc;
							}else{
								dialog({
						  			type:'error',
						  			title:'提示',
						  			content:data.msg,
						  			confirm:function(){
						  				window.location.href="/kpg/index.do";
							  		}
								});
							}
						}
					});
				},
				getActivityBaomingxiangmuById(){
					let that = this;
					$.ajax({
						type: "POST",
						url: ctx + "/getActivityBaomingxiangmuById.do",
						data: {bmId:that.bmId},
						success: function(data) {
							if(data.state){
								that.activityBaomingXiangmu = data.activityBaomingXiangmu;
								that.saveData.studentId = data.activityBaomingXiangmu.xs_id;
								that.saveData.sbr = data.activityBaomingXiangmu.sbr;
								that.saveData.bmzz = data.activityBaomingXiangmu.bmzz;
								that.bmzzList = data.activityBaomingXiangmu.bmzz.split(",");
								that.saveData.ssxx = data.activityBaomingXiangmu.ssxx;
								that.saveData.id = data.activityBaomingXiangmu.id;
								that.$nextTick(function(){
									that.saveData.activityXiangmuId = data.activityBaomingXiangmu.hdxm_id;
									that.getZubie();
								});
								that.keyword = data.activityBaomingXiangmu.bmdw;
								that.saveData.zdls = data.activityBaomingXiangmu.zdls;
								that.saveData.c1 = data.activityBaomingXiangmu.c1;
								that.saveData.c2 = data.activityBaomingXiangmu.c2;
								that.saveData.c3 = data.activityBaomingXiangmu.c3;
								that.saveData.c4 = data.activityBaomingXiangmu.c4;
								that.saveData.c5 = data.activityBaomingXiangmu.c5;
								that.saveData.c6 = data.activityBaomingXiangmu.c6;
							}else{
								dialog({
						  			type:'error',
						  			title:'提示',
						  			content:data.msg
//						  			confirm:function(){
//							  			});
								});
								window.location.href="/kpg/index.do";
							}
						}
					});
				},
				saveSignup(){
					let that = this;
					//校验数据
					if(that.saveData.xuexiaoId == undefined || that.saveData.xuexiaoId == null || that.saveData.xuexiaoId == ""){
						dialog({
				  			type:'error',
				  			title:'提示',
				  			content:"请选择参赛单位",
						});
						return;
					}
					//校验数据
					if(that.saveData.studentId == undefined || that.saveData.studentId == null || that.saveData.studentId == ""){
						dialog({
				  			type:'error',
				  			title:'提示',
				  			content:"该账号关联学生数据有误，请咨询网站客服人员解决",
						});
						return;
					}
					if(that.activity.sfszzzrs == "是"){
						if(that.bmzzList.length<1){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请选择参赛人",
							});
							return;
						}else{
							that.saveData.bmzz = that.bmzzList.join(",");
						}
					}
					if(that.activity.c1){
						if(that.saveData.c1 == undefined || that.saveData.c1 == null || that.saveData.c1 == ""){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请填写"+that.activity.c1,
							});
							return;
						}
					}
					if(that.activity.c2){
						if(that.saveData.c2 == undefined || that.saveData.c2 == null || that.saveData.c2 == ""){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请填写"+that.activity.c2,
							});
							return;
						}
					}
					if(that.activity.c3){
						if(that.saveData.c3 == undefined || that.saveData.c3 == null || that.saveData.c3 == ""){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请填写"+that.activity.c3,
							});
							return;
						}
					}
					if(that.activity.c4){
						if(that.saveData.c4 == undefined || that.saveData.c4 == null || that.saveData.c4 == ""){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请填写"+that.activity.c4,
							});
							return;
						}
					}
					if(that.activity.c5){
						if(that.saveData.c5 == undefined || that.saveData.c5 == null || that.saveData.c5 == ""){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请填写"+that.activity.c5,
							});
							return;
						}
					}
					if(that.activity.c6){
						if(that.saveData.c6 == undefined || that.saveData.c6 == null || that.saveData.c6 == ""){
							dialog({
					  			type:'error',
					  			title:'提示',
					  			content:"请填写"+that.activity.c6,
							});
							return;
						}
					}
					
					
					if(that.saveData.activityXiangmuId == undefined || that.saveData.activityXiangmuId == null || that.saveData.activityXiangmuId == ""){
						dialog({
				  			type:'error',
				  			title:'提示',
				  			content:"请选择报名项目",
						});
						return;
					}
					if(that.saveData.dmid == undefined || that.saveData.dmid == null || that.saveData.dmid == ""){
						dialog({
				  			type:'error',
				  			title:'提示',
				  			content:"请选择活动分组",
						});
						return;
					}
					if(that.saveData.zdls == undefined || that.saveData.zdls == null || that.saveData.zdls == ""){
						dialog({
				  			type:'erro',
				  			title:'提示',
				  			content:"请输入指导老师",
						});
						return;
					}
					dialog({
			  			type:'warn',
			  			title:'提示',
			  			content:"确认提交报名信息吗？",
			  			confirm:function(){
			  				$.ajax({
								type: "POST",
								url: ctx + "/saveOrUpdateSignup.do",
								data: that.saveData,
								success: function(data) {
									if(data.state){
										dialog({
								  			type:'info',
								  			title:'提示',
								  			content:"提交成功",
								  			confirm:function(){
								  				window.location.href = "/kpg/wdkpg.do";
								  			}
										});
									}else{
										dialog({
								  			type:'error',
								  			title:'提示',
								  			content:data.msg
										});
									}
								}
							});
				  		}
					});
				},
				getJoinUnit(){
					let that = this;
					$(".loading").show();
					$.ajax({
						type: "POST",
						url: ctx + "/getJoinUnit.do",
						data: {keyword:that.keyword},
						success: function(data) {
							$(".loading").hide();
							if(data.state){
								that.xueXiaos = data.xueXiaos;
							}else{
								dialog({
						  			type:'error',
						  			title:'提示',
						  			content:data.msg
								});
							}
						}
					});
				},
				getXueshengsByName(){
					let that = this;
					$(".loading").show();
					$.ajax({
						type: "POST",
						url: ctx + "/getXueshengsByName.do",
						data: {studentName:that.studentName},
						success: function(data) {
							$(".loading").hide();
							if(data.state){
								that.xueshengs = data.xueshengs;
							}else{
								dialog({
						  			type:'error',
						  			title:'提示',
						  			content:data.msg
								});
							}
						}
					});
				},
				getZubie(){
					let that = this;
					$(".loading").show();
					$.ajax({
						type: "POST",
						url: ctx + "/getActivityZubie.do",
						data: {activityXiangmuId:that.saveData.activityXiangmuId,activityId:that.activityId},
						success: function(data) {
							$(".loading").hide();
							if(data.state){
								that.daimas = data.daimas;
								that.$nextTick(function(){
									if(that.activityBaomingXiangmu){
										that.saveData.dmid = that.activityBaomingXiangmu.hdzb_id;
									}
								});
							}else{
								alert(data.msg);
							}
						}
					});
				},
				getActivityXiangmus(){
					let that = this;
					$.ajax({
						type: "POST",
						url: ctx + "/getActivityXiangmus.do",
						data: {activityId:that.activityId},
						success: function(data) {
							if(data.state){
								that.activityXiangmus = data.activityXiangmus;
								if(that.bmId != ""){
									that.getActivityBaomingxiangmuById();
								}
							}else{
								alert(data.msg);
							}
						}
					});
				}
			},
			mounted(){
				//选择用户按钮事件
				let that = this;
				$(document).on("click",".m-btn-select",function(){
					$(".m-dialog").show();
					that.studentName = "";
					that.xueshengs = [];
				});
				
				Array.prototype.indexOf = function(val) { 
					for (var i = 0; i < this.length; i++) { 
					if (this[i] == val) return i; 
					} 
					return -1; 
					};

				Array.prototype.remove = function(val) { 
					var index = this.indexOf(val); 
					if (index > -1) { 
					this.splice(index, 1); 
					} 
				};

			}
		}
		var Component = Vue.extend(Main);
		new Component().$mount('#m-container');
});


